#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="Clamp.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2007, 2008, 2009, 2010, 2011
//       Roland Rosenkranz (Glatzemann@email.de)
//
//    Based on libnoise by Jason Bevins
//      Copyright (C) 2003, 2004 Jason Bevins (licensed under LGPL)
// </copyright>
// <license>
//   This file is part of starLiGHT.Noise.
//
//   starLiGHT.Noise is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Noise is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Noise. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Noise are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 10385                 $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2011-06-14 07:59:17 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.Noise.Noise
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;

    #endregion

    /// Noise module that clamps the output value from a source module to a
    /// range of values.
    ///
    /// @image html moduleclamp.png
    ///
    /// The range of values in which to clamp the output value is called the
    /// <i>clamping range</i>.
    ///
    /// If the output value from the source module is less than the lower
    /// bound of the clamping range, this noise module clamps that value to
    /// the lower bound.  If the output value from the source module is
    /// greater than the upper bound of the clamping range, this noise module
    /// clamps that value to the upper bound.
    ///
    /// To specify the upper and lower bounds of the clamping range, call the
    /// SetBounds() method.
    ///
    /// This noise module requires one source module.
    public class Clamp : Module
    {
        /// Lower bound of the clamping range.
        protected double lowerBound;

        /// Upper bound of the clamping range.
        protected double upperBound;

        /// Default lower bound of the clamping range for the noise::module::Clamp
        /// noise module.
        private const double DEFAULT_CLAMP_LOWER_BOUND = -1.0;

        /// Default upper bound of the clamping range for the noise::module::Clamp
        /// noise module.
        private const double DEFAULT_CLAMP_UPPER_BOUND = 1.0;

        private List<Parameter> parameterList;

        /// Constructor.
        ///
        /// The default lower bound of the clamping range is set to
        /// noise::module::DEFAULT_CLAMP_LOWER_BOUND.
        ///
        /// The default upper bound of the clamping range is set to
        /// noise::module::DEFAULT_CLAMP_UPPER_BOUND.
        public Clamp() : base(1)
        {
            this.lowerBound = DEFAULT_CLAMP_LOWER_BOUND;
            this.upperBound = DEFAULT_CLAMP_UPPER_BOUND;
        }

        public override List<Parameter> ParameterList
        {
            get
            {
                if (this.parameterList == null)
                {
                    this.parameterList = new List<Parameter>();
                    this.parameterList.Add(new Parameter("LowerBound", "double", DEFAULT_CLAMP_LOWER_BOUND, -1.0, 1.0));
                    this.parameterList.Add(new Parameter("UpperBound", "double", DEFAULT_CLAMP_UPPER_BOUND, -1.0, 1.0));
                }

                return this.parameterList;
            }
        }

        /// Returns the lower bound of the clamping range.
        ///
        /// @returns The lower bound.
        ///
        /// If the output value from the source module is less than the lower
        /// bound of the clamping range, this noise module clamps that value
        /// to the lower bound.
        public double GetLowerBound()
        {
          return this.lowerBound;
        }

        /// Returns the upper bound of the clamping range.
        ///
        /// @returns The upper bound.
        ///
        /// If the output value from the source module is greater than the
        /// upper bound of the clamping range, this noise module clamps that
        /// value to the upper bound.
        public double GetUpperBound()
        {
          return this.upperBound;
        }

        public override double GetValue(double x, double y, double z, double lowerXBound, double upperXBound, double lowerZBound, double upperZBound)
        {
            System.Diagnostics.Debug.Assert(sourceModule[0] != null, String.Empty);

            double value = sourceModule[0].GetValue(x, y, z, lowerXBound, upperXBound, lowerZBound, upperZBound);
            if (value < this.lowerBound)
            {
                return this.lowerBound;
            }
            else if (value > this.upperBound)
            {
                return this.upperBound;
            }
            else
            {
                return value;
            }
        }

        /// Sets the lower and upper bounds of the clamping range.
        ///
        /// @param lowerBound The lower bound.
        /// @param upperBound The upper bound.
        ///
        /// @pre The lower bound must be less than or equal to the
        /// upper bound.
        ///
        /// @throw noise::ExceptionInvalidParam An invalid parameter was
        /// specified; see the preconditions for more information.
        ///
        /// If the output value from the source module is less than the lower
        /// bound of the clamping range, this noise module clamps that value
        /// to the lower bound.  If the output value from the source module
        /// is greater than the upper bound of the clamping range, this noise
        /// module clamps that value to the upper bound.
        public void SetBounds(double lowerBound, double upperBound)
        {
            ////assert(lowerBound < upperBound);

            this.lowerBound = lowerBound;
            this.upperBound = upperBound;
        }

        public override void SetParameter(string name, object value)
        {
            switch (name.ToLower())
            {
                case "lowerbound":
                    this.SetBounds(Convert.ToDouble(value), this.upperBound);
                    break;
                case "upperbound":
                    this.SetBounds(Convert.ToDouble(value), this.upperBound);
                    break;
            }
        }

        public override object GetParameter(string name)
        {
            switch (name.ToLower())
            {
                case "lowerbound": return this.GetLowerBound();
                case "upperbound": return this.GetUpperBound();
            }

            return null;
        }
    }
}

